home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zdscpars.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  14.3 KB  |  437 lines

  1. /* Copyright (C) 2000 Artifex Software Inc.   All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17.  */
  18.  
  19. /*$Id: zdscpars.c,v 1.3.2.3 2000/11/09 23:43:39 rayjj Exp $ */
  20. /* C language interface routines to DSC parser */
  21.  
  22. /*
  23.  * The DSC parser consists of three pieces.  The first piece is a DSC parser
  24.  * which was coded by Russell Lang (dscparse.c and dscparse.h).  The second
  25.  * piece is this module.  These two are sufficient to parse DSC comments
  26.  * and make them available to a client written in PostScript.  The third
  27.  * piece is a PostScript language module (gs_dscp.ps) that uses certain
  28.  * comments to affect the interpretation of the file.
  29.  *
  30.  * The .initialize_dsc_parser operator defined in this file creates an
  31.  * instance of Russell's parser, and puts it in a client-supplied dictionary
  32.  * under a known name (/DSC_struct).
  33.  *
  34.  * When the PostScript scanner sees a possible DSC comment (first characters
  35.  * in a line are %%), it calls the procedure that is the value of the user
  36.  * parameter ProcessDSCComments.  This procedure should loads the dictionary
  37.  * that was passed to .initialize_dsc_parser, and then call the
  38.  * .parse_dsc_comments operator defined in this file.
  39.  *
  40.  * These two operators comprise the interface between PostScript and C code.
  41.  *
  42.  * There is a "feature" named usedsc that loads a PostScript file
  43.  * (gs_dscp.ps), which installs a simple framework for processing DSC
  44.  * comments and having them affect interpretation of the file (e.g., by
  45.  * setting page device parameters).  See gs_dscp.ps for more information.
  46.  *
  47.  * .parse_dsc_comments pulls the comment string off of the stack and passes
  48.  * it to Russell's parser.  That parser parses the comment and puts any
  49.  * parameter values into a DSC structure.  That parser also returns a code
  50.  * which indicates which type of comment was found.  .parse_dsc_comments
  51.  * looks at the return code and transfers any interesting parameters from
  52.  * the DSC structure into key value pairs in the dsc_dict dictionary.  It
  53.  * also translates the comment type code into a key name (comment name).
  54.  * The key name is placed on the operand stack.  Control then returns to
  55.  * PostScript code, which can pull the key name from the operand stack and
  56.  * use it to determine what further processing needs to be done at the PS
  57.  * language level.
  58.  *
  59.  * To add support for new DSC comments:
  60.  *
  61.  * 1. Verify that Russell's parser supports the comment.  If not, then add
  62.  *    the required support.
  63.  *
  64.  * 2. Add an entry into DSCcmdlist.  This table contains three values for
  65.  *    each command that we support.  The first is Russell's return code for
  66.  *    the command. The second is the key name that we pass back on the
  67.  *    operand stack.  (Thus this table translates Russell's codes into key
  68.  *    names for the PostScript client.)  The third entry is a pointer to a
  69.  *    local function for transferring values from Russell's DSC structure
  70.  *    into key/value pairs in dsc_dict.
  71.  *
  72.  * 3. Create the local function described at the end of the last item.
  73.  *    There are some support routines like dsc_put_integer() and
  74.  *    dsc_put_string() to help implement these functions.
  75.  *
  76.  * 4. If the usedsc feature should recognize and process the new comments,
  77.  *    add a processing routine into the dictionary DSCparserprocs in
  78.  *    gs_dscp.ps.  The keys in this dictionary are the key names described
  79.  *    in item 2 above.
  80.  */
  81.  
  82. #include "ghost.h"
  83. #include "string_.h"
  84. #include "memory_.h"
  85. #include "gsstruct.h"
  86. #include "ialloc.h"
  87. #include "iname.h"
  88. #include "istack.h"        /* for iparam.h */
  89. #include "iparam.h"
  90. #include "ivmspace.h"
  91. #include "oper.h"
  92. #include "estack.h"
  93. #include "store.h"
  94. #include "idict.h"
  95. #include "iddict.h"
  96. #include "dscparse.h"
  97.  
  98. /*
  99.  * Declare the structure we use to represent an instance of the parser
  100.  * as a t_struct.  Currently it just saves a pointer to Russell's
  101.  * data structure.
  102.  */
  103. typedef struct dsc_data_s {
  104.     CDSC *dsc_data_ptr;
  105. } dsc_data_t;
  106.  
  107. /* Structure descriptors */
  108. private void dsc_finalize(P1(void *vptr));
  109. gs_private_st_simple_final(st_dsc_data_t, dsc_data_t, "dsc_data_struct", dsc_finalize);
  110.  
  111. /* Define the key name for storing the instance pointer in a dictionary. */
  112. private const char * const dsc_dict_name = "DSC_struct";
  113.  
  114. /* ---------------- Initialization / finalization ---------------- */
  115.  
  116. /*
  117.  * If we return CDSC_OK then Russell's parser will make it best guess when
  118.  * it encounters unexpected comment situations.
  119.  */
  120. private int
  121. dsc_error_handler(void *caller_data, CDSC *dsc, unsigned int explanation,
  122.           const char *line, unsigned int line_len)
  123. {
  124.     return CDSC_OK;
  125. }
  126.  
  127. /*
  128.  * This operator creates a new, initialized instance of the DSC parser.
  129.  */
  130. /* <dict> .initialize_dsc_parser - */
  131. private int
  132. zinitialize_dsc_parser(i_ctx_t *i_ctx_p)
  133. {
  134.     ref local_ref;
  135.     int code;
  136.     os_ptr const op = osp;
  137.     dict * const pdict = op->value.pdict;
  138.     gs_memory_t * const mem = (gs_memory_t *)dict_memory(pdict);
  139.     dsc_data_t * const data =
  140.     gs_alloc_struct(mem, dsc_data_t, &st_dsc_data_t,
  141.             "DSC parser init");
  142.  
  143.     data->dsc_data_ptr = dsc_init((void *) "Ghostscript DSC parsing");
  144.     if (!data->dsc_data_ptr)
  145.         return_error(e_VMerror);
  146.     dsc_set_error_function(data->dsc_data_ptr, dsc_error_handler);
  147.     make_astruct(&local_ref, a_readonly | r_space(op), (byte *) data);
  148.     code = idict_put_string(op, dsc_dict_name, &local_ref);
  149.     if (code >= 0)
  150.     pop(1);
  151.     return code;
  152. }
  153.  
  154. /*
  155.  * This routine will free the memory associated with Russell's parser.
  156.  */
  157. private void
  158. dsc_finalize(void *vptr)
  159. {
  160.     dsc_data_t * const st = vptr;
  161.  
  162.     if (st->dsc_data_ptr)
  163.     dsc_free(st->dsc_data_ptr);
  164.     st->dsc_data_ptr = NULL;
  165. }
  166.  
  167.  
  168. /* ---------------- Parsing ---------------- */
  169.  
  170. /* ------ Utilities for returning values ------ */
  171.  
  172. /* Return an integer value. */
  173. private int
  174. dsc_put_int(gs_param_list *plist, const char *keyname, int value)
  175. {
  176.     return param_write_int(plist, keyname, &value);
  177. }
  178.  
  179. /* Return a string value. */
  180. private int
  181. dsc_put_string(gs_param_list *plist, const char *keyname,
  182.            const char *string)
  183. {
  184.     gs_param_string str;
  185.  
  186.     param_string_from_transient_string(str, string);
  187.     return param_write_string(plist, keyname, &str);
  188. }
  189.  
  190. /* Return a BoundingBox value. */
  191. private int
  192. dsc_put_bounding_box(gs_param_list *plist, const char *keyname,
  193.              const CDSCBBOX *pbbox)
  194. {
  195.     /* pbbox is NULL iff the bounding box values was "(atend)". */
  196.     int values[4];
  197.     gs_param_int_array va;
  198.  
  199.     if (!pbbox)
  200.     return 0;
  201.     values[0] = pbbox->llx;
  202.     values[1] = pbbox->lly;
  203.     values[2] = pbbox->urx;
  204.     values[3] = pbbox->ury;
  205.     va.data = values;
  206.     va.size = 4;
  207.     va.persistent = false;
  208.     return param_write_int_array(plist, keyname, &va);
  209. }
  210.  
  211. /* ------ Return values for individual comment types ------ */
  212.  
  213. /*
  214.  * These routines transfer data from the C structure into Postscript
  215.  * key/value pairs in a dictionary.
  216.  */
  217. private int
  218. dsc_adobe_header(gs_param_list *plist, const CDSC *pData)
  219. {
  220.     return dsc_put_int(plist, "EPSF", (int)(pData->epsf? 1: 0));
  221. }
  222.  
  223. private int
  224. dsc_creator(gs_param_list *plist, const CDSC *pData)
  225. {
  226.     return dsc_put_string(plist, "Creator", pData->dsc_creator );
  227. }
  228.  
  229. private int
  230. dsc_creation_date(gs_param_list *plist, const CDSC *pData)
  231. {
  232.     return dsc_put_string(plist, "CreationDate", pData->dsc_date );
  233. }
  234.  
  235. private int
  236. dsc_bounding_box(gs_param_list *plist, const CDSC *pData)
  237. {
  238.     return dsc_put_bounding_box(plist, "BoundingBox", pData->bbox);
  239. }
  240.  
  241. private int
  242. dsc_page(gs_param_list *plist, const CDSC *pData)
  243. {
  244.     return dsc_put_int(plist, "PageNum",
  245.                pData->page[pData->page_count - 1].ordinal );
  246. }
  247.  
  248. private int
  249. dsc_pages(gs_param_list *plist, const CDSC *pData)
  250. {
  251.     return dsc_put_int(plist, "NumPages", pData->page_pages);
  252. }
  253.  
  254. private int
  255. dsc_page_bounding_box(gs_param_list *plist, const CDSC *pData)
  256. {
  257.     return dsc_put_bounding_box(plist, "PageBoundingBox", pData->page_bbox);
  258. }
  259.  
  260. /*
  261.  * Translate Russell's defintions of orientation into Postscript's.
  262.  */
  263. private int
  264. convert_orient(CDSC_ORIENTATION_ENUM orient)
  265. {
  266.     return (orient == CDSC_LANDSCAPE ? 1 : 0);
  267. }
  268.  
  269. private int
  270. dsc_page_orientation(gs_param_list *plist, const CDSC *pData)
  271. {
  272.     int page_num = pData->page_count;
  273.  
  274.     /*
  275.      * The pageOrientation comment might be either in the 'defaults'
  276.      * section or in a page section.  If in the defaults then fhe value
  277.      * will be in page_orientation.
  278.      */
  279.     if (page_num && pData->page[page_num - 1].orientation != CDSC_ORIENT_UNKNOWN)
  280.     return dsc_put_int(plist, "PageOrientation",
  281.             convert_orient(pData->page[page_num - 1].orientation));
  282.     else
  283.         return dsc_put_int(plist, "Orientation",
  284.                    convert_orient(pData->page_orientation));
  285. }
  286.  
  287. private int
  288. dsc_orientation(gs_param_list *plist, const CDSC *pData)
  289. {
  290.     return dsc_put_int(plist, "Orientation", 
  291.                convert_orient(pData->page_orientation));
  292. }
  293.  
  294.  
  295. private int
  296. dsc_title(gs_param_list *plist, const CDSC *pData)
  297. {
  298.     return dsc_put_string(plist, "Title", pData->dsc_title );
  299. }
  300.  
  301. private int
  302. dsc_for(gs_param_list *plist, const CDSC *pData)
  303. {
  304.     return 0;                /* To be completed */
  305. }
  306.  
  307. /*
  308.  * This list is used to translate the commment code returned
  309.  * from Russell's DSC parser, define a name, and a parameter procedure.
  310.  */
  311. typedef struct cmd_list_s {
  312.     int code;            /* Russell's DSC parser code (see dsc.h) */
  313.     const char *comment_name;    /* A name to be returned to postscript caller */
  314.     int (*dsc_proc) (P2(gs_param_list *, const CDSC *));
  315.                 /* A routine for transferring parameter values
  316.                    from C data structure to postscript dictionary
  317.                    key/value pairs. */
  318. } cmdlist_t;
  319.  
  320. private const cmdlist_t DSCcmdlist[] = { 
  321.     { CDSC_PSADOBE,        "Header",        dsc_adobe_header },
  322.     { CDSC_CREATOR,        "Creator",        dsc_creator },
  323.     { CDSC_CREATIONDATE,    "CreationDate",    dsc_creation_date },
  324.     { CDSC_TITLE,        "Title",        dsc_title },
  325.     { CDSC_FOR,            "For",        dsc_for },
  326.     { CDSC_BOUNDINGBOX,     "BoundingBox",    dsc_bounding_box },
  327.     { CDSC_ORIENTATION,        "Orientation",    dsc_orientation },
  328.     { CDSC_BEGINDEFAULTS,   "BeginDefaults",    NULL },
  329.     { CDSC_ENDDEFAULTS,     "BeginDefaults",    NULL },
  330.     { CDSC_PAGE,        "Page",        dsc_page },
  331.     { CDSC_PAGES,        "Pages",        dsc_pages },
  332.     { CDSC_PAGEORIENTATION, "PageOrientation",  dsc_page_orientation },
  333.     { CDSC_PAGEBOUNDINGBOX, "PageBoundingBox",    dsc_page_bounding_box },
  334.     { CDSC_EOF,            "EOF",        NULL },
  335.     { 0,            "NOP",        NULL }  /* Table terminator */
  336. };
  337.  
  338. /* ------ Parser interface ------ */
  339.  
  340. /*
  341.  * There are a few comments that we do not want to send to Russell's
  342.  * DSC parser.  If we send the data block type comments, Russell's
  343.  * parser will want to skip the specified block of data.  This is not
  344.  * appropriate for our situation.  So we use this list to check for this
  345.  * type of comment and do not send it to Russell's parser if found.
  346.  */
  347. private const char * const BadCmdlist[] = {
  348.     "%%BeginData:",
  349.     "%%EndData",
  350.     "%%BeginBinary:",
  351.     "%%EndBinary",
  352.     NULL                /* List terminator */
  353. };
  354.  
  355. /* See comments at start of module for description. */
  356. /* <dict> <string> .parse_dsc_comments <dict> <dsc code> */
  357. private int
  358. zparse_dsc_comments(i_ctx_t *i_ctx_p)
  359. {
  360. #define MAX_DSC_MSG_SIZE (DSC_LINE_LENGTH + 4)    /* Allow for %% and CR/LF */
  361.     os_ptr const opString = osp;
  362.     os_ptr const opDict = opString - 1;
  363.     uint ssize;
  364.     int comment_code, code;
  365.     char dsc_buffer[MAX_DSC_MSG_SIZE + 2];
  366.     const cmdlist_t *pCmdList = DSCcmdlist;
  367.     const char * const *pBadList = BadCmdlist;
  368.     ref * pvalue;
  369.     CDSC * dsc_data;
  370.     dict_param_list list;
  371.  
  372.     /*
  373.      * Verify operand types and length of DSC comment string.
  374.      */
  375.     check_type(*opString, t_string);
  376.     check_dict_write(*opDict);
  377.     ssize = r_size(opString);
  378.     if (ssize > MAX_DSC_MSG_SIZE - 2) /* need room for EOL + \0 */
  379.         return (gs_note_error(e_rangecheck));
  380.     /*
  381.      * Pick up the comment string to be parsed.
  382.      */
  383.     memcpy(dsc_buffer, opString->value.bytes, ssize);
  384.     dsc_buffer[ssize] = 0x0d;        /* Russell wants a 'line end' */
  385.     dsc_buffer[ssize + 1] = 0;        /* Terminate string */
  386.     /*
  387.      * Skip data block comments (see comments in front of BadCmdList).
  388.      */
  389.     while (*pBadList && strncmp(*pBadList, dsc_buffer, strlen(*pBadList)))
  390.         pBadList++;
  391.     if (*pBadList) {            /* If found in list, then skip comment */    
  392.         comment_code = 0;        /* Force NOP */
  393.     }
  394.     else {
  395.         /*
  396.          * Parse comments - use Russell Lang's DSC parser.  We need to get
  397.          * data area for Russell Lang's parser.  Note: We have saved the
  398.          * location of the data area for the parser in our DSC dict.
  399.          */
  400.         code = dict_find_string(opDict, dsc_dict_name, &pvalue);
  401.     dsc_data = r_ptr(pvalue, dsc_data_t)->dsc_data_ptr;
  402.         if (code < 0)
  403.             return code;
  404.         comment_code = dsc_scan_data(dsc_data, dsc_buffer, ssize + 1);
  405.         if_debug1('%', "[%%].parse_dsc_comments: code = %d\n", comment_code);
  406.         if (comment_code < 0)
  407.         return_error(comment_code);
  408.     }
  409.     /*
  410.      * Transfer data from DSC structure to postscript variables.
  411.      * Look up proper handler in the local cmd decode list.
  412.      */
  413.     while (pCmdList->code && pCmdList->code != comment_code )
  414.     pCmdList++;
  415.     if (pCmdList->dsc_proc) {
  416.     code = dict_param_list_write(&list, opDict, NULL, iimemory);
  417.     if (code < 0)
  418.         return code;
  419.     code = (pCmdList->dsc_proc)((gs_param_list *)&list, dsc_data);
  420.     iparam_list_release(&list);
  421.     if (code < 0)
  422.         return code;
  423.     }
  424.  
  425.     /* Put DSC comment name onto operand stack (replace string). */
  426.  
  427.     return name_enter_string(pCmdList->comment_name, opString);
  428. }
  429.  
  430. /* ------ Initialization procedure ------ */
  431.  
  432. const op_def zdscpars_op_defs[] = {
  433.     {"1.initialize_dsc_parser", zinitialize_dsc_parser},
  434.     {"2.parse_dsc_comments", zparse_dsc_comments},
  435.     op_def_end(0)
  436. };
  437.